package com.example.jl.bethinner.service;

import android.annotation.TargetApi;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;

import com.bethinner.demo.TestService;
import com.bethinner.utils.TestServiceHelper;
import com.example.jl.bethinner.activity.PushDialogActivity;
import com.example.jl.bethinner.activity.ServiceTestActivity;
import com.example.jl.bethinner.domain.Device;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by JL on 7/17/15.
 */
public class ServiceUpdateUI extends Service {
    public static final String TAG = "ServiceUpdateUI";
    public static final String ACTION = "action.updateUI";
    private Timer timer;
    private TimerTask task;
    private int count;
    private BluetoothAdapter mBluetoothAdapter;
    private Handler mHandler;
    // 3秒后停止查找搜索.
    private static final long SCAN_PERIOD = 3000;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    public void onCreate() {
        super.onCreate();
        mHandler = new Handler();
        final Intent intent = new Intent();
        intent.setAction(ServiceTestActivity.ACTION_UPDATEUI);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            // 初始化 Bluetooth adapter, 通过蓝牙管理器得到一个参考蓝牙适配器(API必须在以上android4.3或以上和版本)
            final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
            mBluetoothAdapter = bluetoothManager.getAdapter();

            // 检查设备上是否支持蓝牙
            if (mBluetoothAdapter == null) {

                return;
            }
            timer = new Timer();
            task = new TimerTask() {

                @Override
                public void run() {
                    intent.putExtra("count", ++count);
                    sendBroadcast(intent);
                    scanLeDevice(true);

                }
            };
            timer.schedule(task, 1000, 10000);
        }
//        startDialogActivity();
    }

    private void startDialogActivity() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent dialogIntent = new Intent(ServiceUpdateUI.this, PushDialogActivity.class);
                dialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(dialogIntent);
            }
        }, 0);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    private void scanLeDevice(final boolean enable) {//查询设备
        if (enable) {
            // Stops scanning after a pre-defined scan period.
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mBluetoothAdapter.stopLeScan(mLeScanCallback);
                }
            }, SCAN_PERIOD);

            mBluetoothAdapter.startLeScan(mLeScanCallback);
        } else {
            mBluetoothAdapter.stopLeScan(mLeScanCallback);

        }
    }

    // Device scan callback.
    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {

        @Override
        public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {


            Device ble = Device.fromScanData(device, rssi, scanRecord);
            if (ble == null)
                return;
            TestService testService = new TestService(ble.getId(), ble.getName());
            TestServiceHelper.getInstance(ServiceUpdateUI.this).addData(testService);

        }
    };
}
